package com.waitingresult;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import com.waitingresult.mapper.UserMapper;
import org.apache.ibatis.annotations.Select;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.lang.reflect.Proxy;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        //动态代理创建对象
        UserMapper mapper = (UserMapper) Proxy.newProxyInstance(Main.class.getClassLoader(), new Class[]{UserMapper.class}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                //  System.err.println(proxy);
//                System.err.println(method.getName());
//                System.err.println(Arrays.asList(args));
//
//                System.err.println("=----");
                Map<String, Object> map = argsMapping(method, args);
//                System.out.println(stringObjectMap);

                Select select = method.getAnnotation(Select.class);
                String sql = select.value()[0];
                parseSql(sql,map);
                //System.out.println(Arrays.asList(sql));
                return null;
            }
        });
        mapper.selectById(1L);
    }

    /**
     * 参数映射
     *
     * @param method
     * @param args
     * @return
     */
    public static Map<String, Object> argsMapping(Method method, Object[] args) {
        HashMap<String, Object> map = new HashMap<>();
        Parameter[] parameters = method.getParameters();
        for (int i = 0; i < parameters.length; i++) {
            String name = parameters[i].getName();
            map.put(name, args[i]);
        }
        return map;
    }


    public static String parseSql(String sql, Map map) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < sql.length(); i++) {
            // 取到每一个字符
            char c = sql.charAt(i);
            if(c == '#'){
                stringBuilder.append(sql.substring(0,i));
                int index = i;
                //得到 {} 中间的值 做替换

            }
        }
        return "";
    }
}